/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.order.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.order.api.entity.OrderInfo;
import com.pig4cloud.pigx.order.api.entity.PointSpuOrder;
import com.pig4cloud.pigx.order.api.vo.PointSpuOrderVO;
import com.pig4cloud.pigx.order.service.PointSpuOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 积分商城订单
 *
 * @author pigx code generator
 * @date 2023-12-19 09:23:55
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/point/spu/order")
@Tag(description = "point_spu_order", name = "积分商城订单管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PointSpuOrderController {

	private final PointSpuOrderService pointSpuOrderService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param pointSpuOrder 积分商城订单
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	public R getPointSpuOrderPage(Page page, PointSpuOrder pointSpuOrder) {
		// return R.ok(pointSpuOrderService.page(page, Wrappers.query(pointSpuOrder)));
		return R.ok(pointSpuOrderService.getPage(page, pointSpuOrder));
	}

	/**
	 * 通过id查询积分商城订单
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(pointSpuOrderService.getOrderInfoByOrderId(id));
	}

	/**
	 * 新增积分商城订单
	 * @param pointSpuOrder 积分商城订单
	 * @return R
	 */
	@Operation(summary = "新增积分商城订单", description = "新增积分商城订单")
	@SysLog("新增积分商城订单")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('point_spu_order_add')")
	public R save(@RequestBody PointSpuOrder pointSpuOrder) {
		return R.ok(pointSpuOrderService.save(pointSpuOrder));
	}

	/**
	 * 修改积分商城订单
	 * @param pointSpuOrder 积分商城订单
	 * @return R
	 */
	@Operation(summary = "修改积分商城订单", description = "修改积分商城订单")
	@SysLog("修改积分商城订单")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('point_spu_order_edit')")
	public R updateById(@RequestBody PointSpuOrder pointSpuOrder) {
		return R.ok(pointSpuOrderService.updateById(pointSpuOrder));
	}

	/**
	 * 通过id删除积分商城订单
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除积分商城订单", description = "通过id删除积分商城订单")
	@SysLog("通过id删除积分商城订单")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('point_spu_order_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(pointSpuOrderService.removeById(id));
	}

	/**
	 * 导出excel 表格
	 * @param pointSpuOrder 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('point_spu_order_export')")
	public List<PointSpuOrder> export(PointSpuOrder pointSpuOrder) {
		return pointSpuOrderService.list(Wrappers.query(pointSpuOrder));
	}

	/**
	 * 创建积分商城订单
	 * @param pointSpuOrderVO
	 * @return
	 */
	@PostMapping("/c/create")
	public R create(@RequestBody PointSpuOrderVO pointSpuOrderVO) {
		Long shopUserId = SecurityUtils.getUser().getId();
		pointSpuOrderVO.setShopUserId(shopUserId);
		return pointSpuOrderService.create(pointSpuOrderVO);
	}

	/**
	 * c端分页获取积分订单
	 * @param page
	 * @param pointSpuOrder
	 * @return
	 */
	@GetMapping("/c/page")
	public R getPointOrderList(Page page, PointSpuOrder pointSpuOrder) {
		Long shopUserId = SecurityUtils.getUser().getId();
		pointSpuOrder.setShopUserId(shopUserId);
		return R.ok(pointSpuOrderService.getPage(page, pointSpuOrder));
	}

	/**
	 * 通过订单ID获取订单详情
	 * @param orderId
	 * @return
	 */
	@GetMapping("/c/orderInfo/{orderId}")
	public R getOrderInfo(@PathVariable(value = "orderId") Long orderId) {
		Long shopUserId = SecurityUtils.getUser().getId();
		return pointSpuOrderService.getOrderInfo(shopUserId, orderId);
	}

	/**
	 * 订单签收
	 * @return
	 */
	@PostMapping("/c/orderSingFor")
	public R orderSingFor(@RequestBody PointSpuOrder pointSpuOrder) {
		Long shopUserId = SecurityUtils.getUser().getId();
		pointSpuOrder.setShopUserId(shopUserId);
		return pointSpuOrderService.orderSingFor(pointSpuOrder);
	}

	/**
	 * 订单退款
	 * @return
	 */
	@PostMapping("/orderRefund")
	@PreAuthorize("@pms.hasPermission('point_spu_order_refund')")
	public R orderRefund(@RequestBody PointSpuOrder pointSpuOrder) {
		return pointSpuOrderService.orderRefund(pointSpuOrder);
	}

	/**
	 * 订单退货
	 * @return
	 */
	@PostMapping("/orderReturn")
	@PreAuthorize("@pms.hasPermission('point_spu_order_return')")
	public R orderReturn(@RequestBody PointSpuOrder pointSpuOrder) {
		return pointSpuOrderService.orderReturn(pointSpuOrder);
	}

}
